package org.esprit.ocm.client.ui.one;

import org.esprit.ocm.client.rpc.oca.VMOcaService;
import org.esprit.ocm.client.rpc.oca.VMOcaServiceAsync;
import org.esprit.ocm.dto.impl.AwsCredentialsDto;
import org.esprit.ocm.dto.impl.ServerDto;
import org.esprit.ocm.dto.impl.UserDto;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.TextAreaItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;

public class CreateVM extends Window {

	private final VMOcaServiceAsync	vmOcaService	= GWT.create(VMOcaService.class);
	
	public CreateVM(final ServerDto server, final AwsCredentialsDto credentials, final UserDto user) {

		setTitle("Create VM");
		setWidth(300);
		setHeight(250);
		setIsModal(true);
		setShowModalMask(true);
		setShowCloseButton(true);
		centerInPage();

		final DynamicForm form = new DynamicForm();
		form.setWidth(250);
		form.setHeight(20);
		form.setPadding(5);
		form.setLayoutAlign(VerticalAlignment.TOP);

		TextItem name = new TextItem("name");
		name.setTitle("VM name");
		name.setRequired(true);

//		ComboBoxItem CB = new ComboBoxItem();
//		CB.setTitle("Select template:");
//		CB.setType("comboBox");
//		CB.setDefaultValue("Please select:");
//		CB.setValueMap("Please select");
		
		final TextAreaItem template = new TextAreaItem();  
		template.setTitle("Template");  

		ButtonItem createButton = new ButtonItem("create", "Create");
		createButton.setAlign(Alignment.CENTER);
		createButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				
				String secret = credentials.getAwsAccessKeyId() + ":" + credentials.getSecretAccessKey();
				String endpoint = server.getIpServer();

				vmOcaService.allocate(secret, endpoint, template.getValue().toString(), new AsyncCallback<String>() {
					
					public void onSuccess(String s) {
						
						SC.warn(s);
						destroy();
					}
					
					public void onFailure(Throwable arg0) {
						
					}
				});
			}
		});

		form.setFields(name, template, createButton);
		form.redraw();

		addItem(form);
		draw();

	}

}
